﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Weazel.Badger;

namespace BadgerTest
{
	/// <summary>
	/// Summary description for EuclidTest
	/// </summary>
	[TestClass]
	public class EuclidTest
	{
		[TestMethod]
		public void TestEuclid()
		{
			string workingDir = "c:\\badger_test_temp\\";
			string description = BadgerTestUtillities.GetResourceFile("Euclid.euclid.fdl");

			// expecting a single warning
			Weazel.Gezel.Model.Model model =
				BadgerTestUtillities.ParseAndVerify(description, 1);

			if (Directory.Exists(workingDir))
				Directory.Delete(workingDir, true);

			Directory.CreateDirectory(workingDir);

			Weazel.Badger.Configuration cfg = new Configuration();
			cfg.OutputFolder = "c:\\badger_test_temp\\";
			cfg.ConvertDisplayDirectives = true;
			cfg.CreateClockAndResetGenerationProcesses = true;		

			Weazel.Badger.ConversionContext conversion =
				new Weazel.Badger.ConversionContext(model, cfg);

			conversion.Convert();

			// assert output has been written			
			Assert.IsTrue(File.Exists(workingDir + "\\" + "S.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "test_euclid.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "euclid.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "std_logic_arithext.vhd"));
			Assert.IsTrue(File.Exists(workingDir + "\\" + "weazel_text_io.vhd"));

			// create "work" library using vlib
			BadgerTestUtillities.RunVLib(workingDir, "work");
			
			// compile vhdl
			BadgerTestUtillities.RunVCom(workingDir, "std_logic_arithext.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "weazel_text_io.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "*.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "*.vhd");
			BadgerTestUtillities.RunVCom(workingDir, "*.vhd");

			// simulate
			BadgerTestUtillities.RunVSim(workingDir, "work.s", "run 10000ns");

			// check that the output files has been written
			Assert.IsTrue(File.Exists(workingDir + "\\" + "euclid_display.txt"));
			
			// finally, compare output with reference
			string reference = BadgerTestUtillities.GetResourceFile("Euclid.euclid_display.txt");
			Assert.AreEqual(reference, BadgerTestUtillities.ReadFile(workingDir + "\\" + "euclid_display.txt"));			 
		}
	}
}
